package com.example.xiaoyuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xiaoyuan.mapper.ClazzMapper;
import com.example.xiaoyuan.pojo.Clazz;
import com.example.xiaoyuan.service.ClazzService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
//@Transactional
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {

    @Override
    public IPage<Clazz> getClazzsByOpr(Page<Clazz> page, Clazz clazz) {

        LambdaQueryWrapper<Clazz> queryWrapper = new LambdaQueryWrapper<>();
        String gradeName = clazz.getGradeName();
        if (!StringUtils.isEmpty(gradeName)){
            queryWrapper.like(Clazz::getGradeName, gradeName);
        }

        String name = clazz.getName();
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like(Clazz::getName, name);
        }

        queryWrapper.orderByAsc(Clazz::getId);

        Page<Clazz> clazzPage = baseMapper.selectPage(page, queryWrapper);

        return clazzPage;
    }

    @Override
    public List<Clazz> getClazzs() {

        List<Clazz> clazzes = baseMapper.selectList(null);

        return clazzes;
    }
}
